package service

import (
	"ksd-social-api/commons/base/service"
	"ksd-social-api/commons/page"
	context3 "ksd-social-api/modules/msg/model/context"
	"ksd-social-api/modules/resume/model"
	"ksd-social-api/modules/resume/model/context"
	"ksd-social-api/modules/resume/model/vo"
	umodel "ksd-social-api/modules/user/model"
	"ksd-social-api/utils"
	"strconv"
	"time"
)

type AdminResumeService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-13
 * @version 1.0
 * @desc 查询资源列表信息，栏目和搜索使用
 */
func (service *AdminResumeService) FindResumeListAndPage(ctx context.ResumeSearchContext) *page.Page {
	p := adminResumeMapper.FindResumeListAndPage(ctx)
	resumes := p.Records.([]model.Resume)
	resumeVos := []vo.ResumeVo{}
	for _, resume := range resumes {
		// 3: 查询当前登录的用户信息和资源id去查询用户资源收藏表
		resumeVo := vo.ResumeVo{}
		utils.CopyProperties(&resumeVo, resume)
		// 2: 如果没有错误就必须把model数据导入vo数据中
		resumeVos = append(resumeVos, resumeVo)
	}

	p.Records = resumeVos
	return p
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminResumeService) SaveUpdateResume(ctx context.ResumeContext) bool {
	// 保存入库
	var resume model.Resume
	utils.CopyProperties(&resume, ctx)
	parseUint, _ := strconv.ParseUint(ctx.Id, 10, 64)
	resume.Id = parseUint
	data := adminResumeMapper.SaveUpdateResume(resume)
	return data != nil
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminResumeService) UpdateFieldById(ctx context.ResumeUpdateContext) bool {
	return adminResumeMapper.UpdateFieldById(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminResumeService) UpdateFieldByCodeId(ctx context.ResumeUpdateContext) bool {
	return adminResumeMapper.UpdateFieldByCodeId(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存和修改兑换码
 */
func (service *AdminResumeService) SaveUpdateCode(ctx context.ResumeCodeContext) *vo.ResumeCodeVO {
	// 保存入库
	var resumeCode model.ResumeCode
	var resumeVoCode vo.ResumeCodeVO
	utils.CopyProperties(&resumeCode, ctx)
	parseUint, _ := strconv.ParseUint(ctx.Id, 10, 64)
	resumeCode.Id = parseUint
	resumeCode.Opid = ctx.Opid.Uint64()
	data := adminResumeMapper.SaveUpdateResumeCode(resumeCode)
	utils.CopyProperties(&resumeVoCode, data)
	return &resumeVoCode
}

/**
 * @author feige
 * @date 2023-10-13
 * @version 1.0
 * @desc 查看兑换码
 */
func (service *AdminResumeService) FindCodePage(ctx context.CodeSearchContext) *page.Page {
	p := adminResumeMapper.FindResumeCodePage(ctx)
	resumeCodes := p.Records.([]model.ResumeCode)
	resumeCodeVos := []vo.ResumeCodeVO{}
	for _, resumeCode := range resumeCodes {
		// 3: 查询当前登录的用户信息和资源id去查询用户资源收藏表
		resumeCodeVo := vo.ResumeCodeVO{}
		utils.CopyProperties(&resumeCodeVo, resumeCode)
		// 2: 如果没有错误就必须把model数据导入vo数据中
		resumeCodeVos = append(resumeCodeVos, resumeCodeVo)
	}
	p.Records = resumeCodeVos
	return p
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据id删除
 */
func (service *AdminResumeService) DelById(id uint64, systemId uint64) bool {
	return adminResumeMapper.DelById(id, systemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据用户id查询用户明细
 */
func (service *AdminResumeService) DelCodeById(id uint64, systemId uint64) bool {
	return adminResumeMapper.DelCodeById(id, systemId)
}

/**
 * 批量授权用户
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *AdminResumeService) SaveUserResumeOrder(ctx context.BatchUsersContext) bool {
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	userVo := userMapper.GetUserInfoByUuid(ctx.Uuid, ctx.SystemId)
	userId := userVo.Id
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := resumeMapper.CountUserBuyResumeIdNo(userId, ctx.Opid.Uint64(), ctx.SystemId)
	if isBuyFlag == 0 {
		// 根据小册id查询小册信息
		resume, _ := resumeMapper.GetById(ctx.Opid.Uint64(), ctx.SystemId)
		resumeNew := model.Resume{}
		resumeNew.Id = ctx.Opid.Uint64()
		OutTradeNo := service.GetSnowWorkerIdString(8)
		resumeNew.TradeNo = OutTradeNo
		resumeNew.PayMethod = "人工服务"
		resumeNew.Code = ""
		resumeNew.SystemId = ctx.SystemId
		resumeNew.Price = resume.Price
		// 保存订单的接口
		resumeMapper.UpdateResume(&resumeNew)
		// 增加会员

		// 根据课程id查询课程信息
		userVipVo := userVipMapper.GetUserVipDetail(resume.RType, ctx.SystemId)
		userVo := userMapper.GetUserByID(userId, ctx.SystemId)
		var userBuyVip umodel.UserBuyVip
		userBuyVip.SystemId = ctx.SystemId
		userBuyVip.SystemName = ctx.SystemName
		// 处理返回结果
		userBuyVip.VipId = resume.RType
		userBuyVip.Title = userVipVo.Title
		userBuyVip.Description = userVipVo.Note

		userBuyVip.UserId = userId
		userBuyVip.Username = userVo.Username
		userBuyVip.Avatar = userVo.Avatar
		userBuyVip.Uuid = userVo.Uuid
		userBuyVip.Phone = userVo.Telephone
		userBuyVip.Address = userVo.Address
		userBuyVip.Nickname = userVo.Nickname

		userBuyVip.Code = ""
		userBuyVip.PayMethod = 3
		userBuyVip.PayMethodName = "兑换码"
		userBuyVip.Tradeno = OutTradeNo
		userBuyVip.Orderno = OutTradeNo
		userBuyVip.OrderJson = ""
		userBuyVip.Price = userVipVo.Price
		// 保存订单的接口
		userBuyVipMapper.SaveUserBuyVip(userBuyVip)

		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = resume.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = ctx.SystemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		messageMeContext.Url = "https://www.study512.com/resume"
		messageMeService.SaveMessageMeBuyResume(&messageMeContext)
		return true
	} else {
		return false
	}
}

/*
复制
*
*/
func (service *AdminResumeService) CopyData(id uint64, systemId uint64) bool {
	// 保存入库
	data := adminResumeTemplateMapper.GetById(id, systemId)
	data.Id = 0
	data.Status = 0
	data.CreateTime = time.Now()
	data.UpdateTime = time.Now()
	adminResumeTemplateMapper.SaveUpdateResumeTemplate(*data)
	return true
}
